package com.example.permissionservice.controller;

import com.example.permissionservice.common.Result;
import com.example.permissionservice.service.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Tag(name = "权限模块",description = "处理用户角色绑定，以及角色相关操作")
public class UserRoleController {
    @Autowired
    private  UserRoleService userRoleService;
    @Operation(summary = "用户绑定角色")
    @PostMapping("/bind/{id}")
    public Result<Void> bindDefaultRole(@PathVariable("id") Long UserId){
        userRoleService.bindDefaultRole(UserId);
        return Result.success();
    }
    @Operation(summary = "根据用户Id获取用户角色")
    @GetMapping("/code/{userId}")
    public Result<String> getUserRoleCode(@PathVariable("userId") Long userId){
        return Result.success(userRoleService.getUserRoleCode(userId));
    }
    @Operation(summary = "将普通用户升级至管理员")
    @PutMapping("/upgrade/{userId}")
    public Result<Void> upgradeToAdmin(@PathVariable("userId") Long userId){
        userRoleService.upgradeToAdmin(userId);
        return Result.success();
    }
    @Operation(summary = "将管理员降级至普通用户")
    @PutMapping("/downgrade/{userId}")
    public Result<Void> downgradeToUser(@PathVariable("userId") Long userId){
        userRoleService.downgradeToUser(userId);
        return Result.success();
    }
    @Operation(summary = "获取所有普通用户的userId")
    @GetMapping("/users")
    public Result<List<Long>> getUser(){
        return Result.success(userRoleService.getUsers());
    }
}
